
#include "BUtils.h"
using namespace blueberry;

UTLMStringTokenizer::UTLMStringTokenizer(char * zSource, char cDelim)
{
	Split(zSource, cDelim);
}
UTLMStringTokenizer::UTLMStringTokenizer()
{

}
void UTLMStringTokenizer::Split(char * _zSource, char _cDelim)
{
	if (_zSource)
	{
		int iLen = strlen(_zSource);
		int iPos = 0;
		for (int i = 0; i < iLen; ++i)
		{
			if (_zSource[i] == _cDelim)
			{
				_zSource[i] = 0;
				vec_tokens.push_back(_zSource + iPos);
				iPos = i + 1;
			}
		}
		vec_tokens.push_back(_zSource + iPos);
	}
}

char * UTLMStringTokenizer::GetTokenAt(int iIndex)
{
	if (iIndex < (int) vec_tokens.size())
		return vec_tokens[iIndex];
	else
		return NULL;
}

int UTLMStringTokenizer::SplitLines(char * _zSource)
{
	char cDelim = '\n';
	int iProcessedLen = -1;
	if (_zSource)
	{
		int iLen = strlen(_zSource);
		int iPos = 0;
		for (int i = 0; i < iLen; ++i)
		{
			if (_zSource[i] == cDelim)
			{
				_zSource[i] = 0;
				vec_tokens.push_back(_zSource + iPos);
				iPos = i + 1;
				iProcessedLen = i;
			}
		}
		//vec_tokens.push_back(_zSource + iPos);
	}
	return ++iProcessedLen;
}

UTLUnicodeStringTokenize::UTLUnicodeStringTokenize(UnicodeString * _pStr, UShort usDelim)
{
	Split(_pStr,usDelim);
}
UTLUnicodeStringTokenize::UTLUnicodeStringTokenize()
{

}
UTLUnicodeStringTokenize::~UTLUnicodeStringTokenize()
{
	for (T_VEC_USTR::iterator ite = vec_tokens.begin(); ite != vec_tokens.end(); ++ite)
	{
		UnicodeString * pUStr = *ite;
		delete pUStr;
	}
	vec_tokens.clear();
}

UnicodeString * UTLUnicodeStringTokenize::GetTokenAt(int iIndex)
{
	if (iIndex < (int) vec_tokens.size())
		return vec_tokens[iIndex];
	else
		return NULL;
}
void UTLUnicodeStringTokenize::Split(UnicodeString * _pStr, UShort _usDelim)
{
	if (_pStr)
	{
		UShort iLen = _pStr->us_StringLen;
		UShort iPos = 0;
		for (UShort i = 0; i < iLen; ++i)
		{
			if (_pStr->aus_String[i] == _usDelim)
			{
				_pStr->aus_String[i] = 0;
				vec_tokens.push_back(new UnicodeString(_pStr,iPos, i));
				iPos = (UShort)( i + 1);
			}
		}
		vec_tokens.push_back(new UnicodeString(_pStr,iPos, (UShort)(iLen - 1)));
	}
}

IPAddrConverter::IPAddrConverter()
{

}
UInt IPAddrConverter::GetIPAdderValue(char * _zIPAddr)
{
	UTLMStringTokenizer oTok(_zIPAddr, '.');
	if ( oTok.GetCount() ==  4)
	{
		return ((atoi(oTok.GetTokenAt(0))) << 24) | ((atoi(oTok.GetTokenAt(1))) << 16) | ((atoi(oTok.GetTokenAt(2))) << 8) | (atoi(oTok.GetTokenAt(3)));
	}
	return 0;
}
void IPAddrConverter::GetIPAddr(char * _zIPAddr, UInt _uiIPAddrValue)
{
	sprintf(_zIPAddr,"%d.%d.%d.%d",((_uiIPAddrValue >> 24) & 0xFF),((_uiIPAddrValue >> 16)& 0xFF), ((_uiIPAddrValue >> 8)& 0xFF), ((_uiIPAddrValue & 0xFF) ));
}
